package dao;

import entity.Order;
import entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class OrderDao {
    //增
    public int add(Order order, Connection connection) throws SQLException {
        String sql = "insert into order_info(community_id,count_key) values (?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, order.getCommunityId());
        statement.setInt(2,order.getCountKey());
        int count = statement.executeUpdate();
        statement.close();
        return count;
    }




    //删
    public int delete(int orderId, Connection connection) throws SQLException {
        String sql = "delete from order_info where order_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,orderId);

        int count = statement.executeUpdate();


        statement.close();
        return count;

    }





    //改
        public int update(Order order, Connection connection) throws SQLException {
            String sql = "update order_info set community_id = ?,count_key = ? where order_id = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, order.getCommunityId());
            statement.setInt(2, order.getCountKey());
            statement.setInt(3, order.getOrderId());
            int count = statement.executeUpdate();
            statement.close();
            return count;
        }
    //查
    public Order selectById(int orderId, Connection connection) throws SQLException {
        String sql = "select * from order_info where order_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, orderId);
        ResultSet resultSet = statement.executeQuery();
        Order order = null;
        while (resultSet.next()){
            int communityId = resultSet.getInt("community_id");
            int countId = resultSet.getInt("count_key");
            order = new Order(orderId,communityId,countId);
            break;
        }



        resultSet.close();
        statement.close();
        return order;
    }
}
